package org.ohos.rajawali3d.examples.examples.animation;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.animation.mesh.SkeletalAnimationObject3D;
import org.rajawali3d.animation.mesh.SkeletalAnimationSequence;
import org.rajawali3d.lights.DirectionalLight;
import org.rajawali3d.loader.ParsingException;
import org.rajawali3d.loader.md5.LoaderMD5Anim;
import org.rajawali3d.loader.md5.LoaderMD5Mesh;

public class SkeletalAnimationBlendingFragment extends AExampleFragment {

    @Override
    public Component onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        inflater.parse(ResourceTable.Layout_skeletal_blending_button_bar, mLayout, true);

        Button button1 = (Button) mLayout.findComponentById(ResourceTable.Id_button1);
        button1.setClickedListener(this);

        Button button2 = (Button) mLayout.findComponentById(ResourceTable.Id_button2);
        button2.setClickedListener(this);

        Button button3 = (Button) mLayout.findComponentById(ResourceTable.Id_button3);
        button3.setClickedListener(this);

        Button button4 = (Button) mLayout.findComponentById(ResourceTable.Id_button4);
        button4.setClickedListener(this);

        inflater.parse(ResourceTable.Layout_skeletal_blending_creator_bar, mLayout, true);
        mLayout.moveChildToFront(mLayout.findComponentById(ResourceTable.Id_button_group));
        mRenderSurface.setTransparent(false);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);
        return mLayout;
    }

    @Override
    public AExampleRenderer createRenderer() {
        return new SkeletalAnimationBlendingRenderer(getContext(), this);
    }

    @Override
    public void onClick(Component v) {
        switch (v.getId()) {
            case ResourceTable.Id_button1:
                ((SkeletalAnimationBlendingRenderer) mRenderer).transitionAnimation(0);
                break;
            case ResourceTable.Id_button2:
                ((SkeletalAnimationBlendingRenderer) mRenderer).transitionAnimation(1);
                break;
            case ResourceTable.Id_button3:
                ((SkeletalAnimationBlendingRenderer) mRenderer).transitionAnimation(2);
                break;
            case ResourceTable.Id_button4:
                ((SkeletalAnimationBlendingRenderer) mRenderer).transitionAnimation(3);
                break;
            default:
        }
    }

    private final class SkeletalAnimationBlendingRenderer extends AExampleRenderer {
        private DirectionalLight mLight;
        private SkeletalAnimationObject3D mObject;
        private SkeletalAnimationSequence mSequenceWalk;
        private SkeletalAnimationSequence mSequenceIdle;
        private SkeletalAnimationSequence mSequenceArmStretch;
        private SkeletalAnimationSequence mSequenceBend;

        SkeletalAnimationBlendingRenderer(Context context, AExampleFragment fragment) {
            super(context, fragment);
        }

        @Override
        protected void initScene() {
            mLight = new DirectionalLight(0, -0.2f, -1.0f); // set the direction
            mLight.setColor(1.0f, 1.0f, .8f);
            mLight.setPower(1);

            getCurrentScene().addLight(mLight);
            getCurrentCamera().setZ(8);

            try {
                ResourceManager resManager = mContext.getResourceManager();
                RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/ingrid_mesh.md5mesh");
                LoaderMD5Mesh meshParser = new LoaderMD5Mesh(this, rawFileEntry);
                meshParser.parse(mContext);

                RawFileEntry rawFileEntry2 = resManager.getRawFileEntry("resources/rawfile/ingrid_idle.md5anim");
                LoaderMD5Anim animParser = new LoaderMD5Anim("idle", this,
                        rawFileEntry2);
                animParser.parse(mContext);

                RawFileEntry rawFileEntry3 = resManager.getRawFileEntry("resources/rawfile/ingrid_walk.md5anim");
                mSequenceIdle = (SkeletalAnimationSequence) animParser
                        .getParsedAnimationSequence();

                animParser = new LoaderMD5Anim("walk", this, rawFileEntry3);
                animParser.parse(mContext);

                mSequenceWalk = (SkeletalAnimationSequence) animParser
                        .getParsedAnimationSequence();

                RawFileEntry rawFileEntry4 = resManager.getRawFileEntry("resources/rawfile/ingrid_arm_stretch.md5anim");
                animParser = new LoaderMD5Anim("armstretch", this,
                        rawFileEntry4);
                animParser.parse(mContext);
                RawFileEntry rawFileEntry5 = resManager.getRawFileEntry("resources/rawfile/ingrid_bend.md5anim");
                mSequenceArmStretch = (SkeletalAnimationSequence) animParser
                        .getParsedAnimationSequence();

                animParser = new LoaderMD5Anim("bend", this, rawFileEntry5);
                animParser.parse(mContext);

                mSequenceBend = (SkeletalAnimationSequence) animParser
                        .getParsedAnimationSequence();

                mObject = (SkeletalAnimationObject3D) meshParser
                        .getParsedAnimationObject();
                mObject.setAnimationSequence(mSequenceIdle);
                mObject.setFps(24);
                mObject.setScale(.8f);
                mObject.play();

                getCurrentScene().addChild(mObject);
            } catch (ParsingException e) {
                e.printStackTrace();
            }
        }

        public void transitionAnimation(int id) {
            switch (id) {
                case 0:
                    mObject.transitionToAnimationSequence(mSequenceIdle, 1000);
                    break;
                case 1:
                    mObject.transitionToAnimationSequence(mSequenceWalk, 1000);
                    break;
                case 2:
                    mObject.transitionToAnimationSequence(mSequenceArmStretch, 1000);
                    break;
                case 3:
                    mObject.transitionToAnimationSequence(mSequenceBend, 1000);
                    break;
            }
        }

    }
}
